package com.matt.controller.group;

import com.dotflat.safe.domain.SterilizationRecord;
import com.dotflat.safe.service.SterilizationRecordService;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.foundation.model.ErrorCode;
import com.matt.service.remoteCall.DTO.SsoAreaDTO;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.matt.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/web/group/staff")
@Slf4j
@Api(tags = "Web集团 - 员工查询")
public class WebGroupStaffController {

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private SterilizationRecordService recordService;


    @GetMapping("/list")
    @ApiOperation(value = "列表集团下所有员工", response = SterilizationRecord.class)
    public StatusResponse getList(@RequestHeader(TICKET) String ticket,
                                  String areaId, String roleId, String realName,
                                    PagerQuery pagerQuery ){
        Map result = ssoCallService.getGroupAllStaff(ticket, areaId, roleId, realName, pagerQuery.getPageNum(), pagerQuery.getPageSize());

        return StatusResponse.success(result);
    }

    @ApiOperation(value = "获取员工基础信息",httpMethod = "POST")
    @RequestMapping("/base_info")
    public StatusResponse getBaseInfo(String staffId) {
        StaffDTO staff = ssoCallService.getStaffDTO(staffId);
        System.out.println("====realName=====:"+staff.getRealName());
        return StatusResponse.success(staff);
    }

    @ApiOperation(value = "员工离职",httpMethod = "POST")
    @RequestMapping("/quit")
    public StatusResponse staffQuit(@RequestHeader String ticket, String areaId, String staffId){

        List<SsoAreaDTO> areaDTOList = ssoCallService.getGroupAllArea(ticket);

        Optional<SsoAreaDTO> op = areaDTOList.stream().filter(o->o.getId().equals(areaId)).findFirst();

        if (!op.isPresent()){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "指定园区不存在");
        }

        return StatusResponse.success(ssoCallService.quitStaff(ticket, areaId, staffId));
    }

    @ApiOperation(value = "员工入职",httpMethod = "POST")
    @RequestMapping("/entry")
    public StatusResponse staffEntry(@RequestHeader String ticket, String areaId, String staffId){
        List<SsoAreaDTO> areaDTOList = ssoCallService.getGroupAllArea(ticket);

        Optional<SsoAreaDTO> op = areaDTOList.stream().filter(o->o.getId().equals(areaId)).findFirst();

        if (!op.isPresent()){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "指定园区不存在");
        }

        return StatusResponse.success(ssoCallService.staffEntry(ticket, areaId, staffId));
    }
}
